package com.darrenchan.leetcode;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2020-02-23 20:18
 */
public class QLint596 {
    int min = Integer.MAX_VALUE;
    TreeNode minNode = null;
    public TreeNode findSubtree(TreeNode root) {
        // write your code here
        helper(root);
        return minNode;
    }

    private int helper(TreeNode root) {
        if(root == null) {
            return 0;
        }

        int left = helper(root.left);
        int right = helper(root.right);

        if(left + right + root.val < min) {
            min = left + right + root.val;
            minNode = root;
        }
        return left + right + root.val;
    }

}
